package com.ncmmall.api.apis.mall;


import com.ncmmall.api.apis.service.Result;
import com.ncmmall.api.security.Securitys;
import com.ncmmall.application.business.bankcard.BankCardApplication;
import com.ncmmall.domain.business.bankcard.BankCard;
import com.ncmmall.domain.business.bankcard.QBankCard;
import com.ncmmall.domain.security.user.WebUser;
import com.ncmmall.querychannel.service.QueryChannelService;
import com.google.common.collect.ImmutableMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
 * 银行卡接口
 *
 * @author likaikai
 * @date 2019-07-15 11:58:06
 */
@RestController
@RequestMapping(value = "/bankCard")
@Component
public class BankCardApi {

    private Logger logger = LoggerFactory.getLogger(BankCardApi.class);

    @Autowired
    private QueryChannelService queryChannelService;

    @Autowired
    private BankCardApplication bankCardApplication;


    /**
     * 银行列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/banks", method = RequestMethod.GET)
    @ResponseBody
    public Result banks() {
        Result result = new Result();

        List<String> list = new ArrayList<>();
        Collections.addAll(list, "中国工商银行", "招商银行", "中国光大银行", "中信银行", "浦发银行", "广发银行", "华夏银行", "中国建设银行", "交通银行", "中国银行", "中国民生银行", "兴业银行", "平安银行", "中国农业银行", "中国邮政储蓄银行");

        return result.ok().inject(list);
    }


    /**
     * 银行卡列表
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @ResponseBody
    public Result list(HttpServletRequest request) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);

            List<BankCard> bankCards = queryChannelService.queryAllEntity("select * from mall_bank_card where member_id = :mid", ImmutableMap.of("mid", member.getId()), BankCard.class);

            return result.ok().inject(bankCards);

        } catch (Exception e) {

            logger.error("[获取银行卡列表异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }


    /**
     * 银行卡详情
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/detail", method = RequestMethod.GET)
    @ResponseBody
    public Result detail(HttpServletRequest request, long id) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);

            BankCard bankCard = queryChannelService.queryOneEntity("select * from mall_bank_card where member_id = :mid and id = :id", ImmutableMap.of("mid", member.getId(), "id", id), BankCard.class);

            return result.ok().inject(bankCard);

        } catch (Exception e) {

            logger.error("[获取银行卡详情异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }


    /**
     * 添加银行卡
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ResponseBody
    public Result create(HttpServletRequest request, @RequestBody BankCard params) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);

            int count = queryChannelService.selectCount("select count(*) from mall_bank_card where bankCardNo = :bankCardNo", ImmutableMap.of("bankCardNo", params.getBankCardNo()));

            if (count > 0) {
                return result.fail().setMsg("银行卡号已存在");
            }

            params.setMember(member);

            bankCardApplication.create(params);
            return result.ok();

        } catch (Exception e) {

            logger.error("[绑定银行卡异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }


    /**
     * 编辑银行卡
     *
     * @param params
     * @return
     */
    @RequestMapping(value = "/modify", method = RequestMethod.PUT)
    @ResponseBody
    public Result collect(HttpServletRequest request, @RequestBody BankCard params) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);

            BankCard bankCard = queryChannelService.findOne(QBankCard.bankCard.member.id.eq(member.getId()).and(QBankCard.bankCard.id.eq(params.getId())), BankCard.class);
            if (bankCard == null) {
                return result.fail().setMsg("银行卡不存在");
            }

            int count = queryChannelService.selectCount("select count(*) from mall_bank_card where bankCardNo = :bankCardNo and id != :bid", ImmutableMap.of("bankCardNo", params.getBankCardNo(), "bid", params.getId()));

            if (count > 0) {
                return result.fail().setMsg("银行卡号已存在");
            }

            bankCardApplication.modify(bankCard, params);

            return result.ok();

        } catch (Exception e) {

            logger.error("[编辑银行卡异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }


    /**
     * 删除银行卡
     *
     * @param params
     * @param id     银行卡id
     * @return
     */
    @RequestMapping(value = "/remove", method = RequestMethod.DELETE)
    @ResponseBody
    public Result cancelCollect(HttpServletRequest request, long id) {
        Result result = new Result();
        try {

            WebUser member = Securitys.user(request);

            BankCard bankCard = queryChannelService.findOne(QBankCard.bankCard.member.id.eq(member.getId()).and(QBankCard.bankCard.id.eq(id)), BankCard.class);
            if (bankCard == null) {
                return result.fail().setMsg("银行卡不存在");
            }

            bankCardApplication.remove(bankCard);

            return result.ok();

        } catch (Exception e) {

            logger.error("[银行卡解绑异常]", e);
            return result.fail().setMsg("网络连接失败");
        }

    }


}
